// pages/gjIndex/stepCar/stepCar.js
import config from "../../../config.js";
var util = require('../../../utils/util.js');
const openIdUrl = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [],
    typeIndex: 0,
    carIndex: 0,
    carList: [],
    orderType: 0,
    addressPick: "",
    addressReturn: "",
    backAddress: "",
    getAddress: "",
    backAddressIndex: 0,
    getAddressIndex: 0,
    locLatitudePick: 0,
    locLongitudePick: 0,
    locLatitudeReturn: 0,
    locLongitudeReturn: 0,
    car: null,
    user: null,
    date: util.getDate(),
    mindate: util.getDate(),
    time: util.getMinute(),
    mintime: util.getMinute(),
    date2: util.getDate(),
    mindate2: util.getDate(),
    time2: util.getMinute(),
    mintime2: util.getMinute(),
    // 是否上牌
    hasNo: true,
    stepCarLocList: [],
    stepCarLocPickList: [],
    stepCarLocBackList: [],
    userId: null,
    orderId: -1,
    order: null,
    canChange: 1,
    //行驶证
    licenseFr: "https://app.longchg.com/gudongcar/images/licenseA.png",
    licenseBa: "https://app.longchg.com/gudongcar/images/licenseB.png",
    //驾驶证
    idenCardA: "https://app.longchg.com/gudongcar/images/idenCardA.png",
    idenCardB: "https://app.longchg.com/gudongcar/images/idenCardB.png",
    isUploadFr: false,
    isUploadBa: false,
    isUploadIDFr: false,
    isUploadIDBa: false,
    isUploadFirst: true,
    isUploadBaFirst: true,
    isUploadIDFirst: true,
    isUploadBaIDFirst: true,
  },
  dateChange: function(e) {
    var time = util.getMinute();
    var date = e.detail.value;
    var currentDate = util.getDate();
    var mintime = this.data.mintime;



    if (date != currentDate) {
      mintime = "";
    } else {
      mintime = util.getMinute();
      this.setData({
        time: time
      })
    }


    this.setData({
      date: e.detail.value,
      mintime: mintime,
    })
  },
  getStepCarLocList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.stepCarLocList,
      data: {},
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          that.setData({
            stepCarLocList: res.data.data,
            stepCarLocPickList: res.data.data[0].stepcarlocations,
            stepCarLocBackList: res.data.data[1].stepcarlocations,
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  },

  timeChange: function(e) {
    this.setData({
      time: e.detail.value,
    })
  },
  date2Change: function(e) {
    var time = util.getMinute();
    var date = e.detail.value;
    var currentDate = util.getDate();
    var mintime = this.data.mintime;



    if (date != currentDate) {
      mintime = "";
    } else {
      mintime = util.getMinute();
      this.setData({
        time2: time
      })
    }


    this.setData({
      date2: e.detail.value,
      mintime2: mintime,
    })
  },

  time2Change: function(e) {
    this.setData({
      time2: e.detail.value,
    })
  },

  carChange: function(e) {
    this.setData({
      carIndex: e.detail.value,
    })
  },

  getTypeList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.stepServiceTypeList,
      data: {
        start: 0,
        type: 1
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 200) {
          that.setData({
            typeList: res.data.data,
            typeIndex: 0
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  },
  getCarList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.stepCarInfo,
      data: {
        // id: 1,
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data.content);
          that.setData({
            carList: res.data.data
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  },
  submit: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var user = wx.getStorageSync("user");
    var houseKeeperId = -1;
    if (that.data.order!=null){
      houseKeeperId = that.data.order.order.housekeeper.keeperId;
    }

    var orderType = 7;
    var serviceType = that.data.typeList[that.data.typeIndex].typeId;
    var stepCarId = that.data.carList[that.data.carIndex].carId;
    var comId = 0;
    var orderFrom = 2;
    var addressReturn = that.data.getAddress;

    var tel = this.data.user.tel;
    var pickTime = that.data.date + " " + that.data.time;
    var returnTime = that.data.date2 + " " + that.data.time2;

    var addressReturn = that.data.stepCarLocList[1].stepcarlocations[that.data.backAddressIndex].locId;
    var addressPick = that.data.stepCarLocList[0].stepcarlocations[that.data.getAddressIndex].locId;

    var isUploadFr = this.data.isUploadFr,
      isUploadBa = this.data.isUploadBa,
      isUploadIDFr = this.data.isUploadIDFr,
      isUploadIDBa = this.data.isUploadIDBa,
      licenseFr = this.data.licenseFr,
      licenseBa = this.data.licenseBa,
      idenCardA = this.data.idenCardA,
      idenCardB = this.data.idenCardB;

    if (this.data.car==null){
      wx.showToast({
        title: '请选择用户车辆',
        icon: 'none',
      })
      return;
    }

    if (!isUploadIDFr) {
      wx.showToast({
        title: '请选择行驶证正面',
        icon: 'none',
      })
      return;
    } else if (!isUploadIDBa) {
      wx.showToast({
        title: '请选择行驶证反面',
        icon: 'none',
      })
      return;
    } else if (!isUploadFr) {
      wx.showToast({
        title: '请选择驾驶证正面',
        icon: 'none',
      })
      return;
    } else if (!isUploadBa) {
      wx.showToast({
        title: '请选择驾驶证反面',
        icon: 'none',
      })
      return;
    }


    wx.request({
      url: openIdUrl.stepSubbmit,
      data: {
        userId: this.data.user.aid,
        orderId: that.data.orderId,
        houseKeeperId: houseKeeperId,
        orderType: orderType,
        serviceType: serviceType,
        stepCarId: stepCarId,
        carId: this.data.car.cid,
        comId: comId,
        pickTime: pickTime,
        returnTime: returnTime,
        orderFrom: orderFrom,
        pickLocId: addressReturn,
        addressPick: addressPick,
        returnLocId: addressPick,
        tel: tel,
        locLatitudeReturn: that.data.locLatitudeReturn,
        locLongitudeReturn: that.data.locLongitudeReturn,
        locLatitudePick: that.data.locLatitudePick,
        locLongitudePick: that.data.locLongitudePick,
        name: that.data.user.name,
        licenseFr: licenseFr,
        licenseBa: licenseBa,
        idenCardA: idenCardA,
        idenCardB: idenCardB
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          wx.setStorageSync("orderId", res.data.data.oid);
          wx.setStorageSync("orderConfirmType", 1);
          wx.reLaunch({
            url: "../gjorder/gjorder",
          })
          wx.showLoading({
            title: '订单更改成功！',
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })

  },
  typeChange: function(e) {
    this.setData({
      typeIndex: e.detail.value,
    })
  },

  servChange: function(e) {
    this.setData({
      servIndex: e.detail.value,
    })
  },

  backChange: function(e) {
    this.setData({
      backAddress: e.detail.value,
    })
  },
  getAddress: function(e) {
    this.setData({
      getAddressIndex: e.detail.value,
    })
  },
  backAddress: function(e) {
    this.setData({
      backAddressIndex: e.detail.value,
    })
  },
  getChange: function(e) {
    this.setData({
      getAddress: e.detail.value,
    })
  },

  selectNo: function() {
    let flag = this.data.hasNo;
    if (flag) {
      this.setData({
        hasNo: false
      })
    } else {
      this.setData({
        hasNo: true
      })
    }
  },

  // 扫描
  scanLicen: function() {
    wx.scanCode({
      scanType: ["barCode", "qrCode", "datamatrix", "pdf417"],
      success: function() {
        //扫描成功
      }
    })
  },

  //上传
  chooseLicenPic: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          uploadImg: tempFilePaths
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var canChange = options.canChange;
    that.setData({
      canChange: canChange,
      date: util.getDate(),
      time: util.getMinute(),
      date2: util.getDate(),
      time2: util.getMinute(),
    })

    this.getTypeList();
    setTimeout(function() {
      that.getStepCarLocList();
    }, 200);



    var that = this;
    //判断是否是再次下单或者更改订单类型
    var orderId = options.orderId;
    var userId = options.uid;

    console.log(orderId);
    console.log(userId);
    if (orderId != null) {
      that.setData({
        orderId: orderId
      })

      //获取订单详情

      setTimeout(function() {
        that.orderDetail();
      }, 300);
    }


  },

  orderDetail() {
    var that = this;
    //获取订单详情
    wx.request({
      url: openIdUrl.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId,
        isKeeper: 1
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            order: res.data.data,
            user: res.data.data.order.account
          })

          var item = res.data.data.order.account;
          if (item.licenseA != null && item.licenseB != null) {
            //设置默认车辆图片信息
            console.log(item);
            that.setData({
              idenCardA: item.licenseA,
              idenCardB: item.licenseB,
              isUploadIDFr: true,
              isUploadIDBa: true,
              isUploadFr: true,
              isUploadBa: true,
              isUploadFirst: false,
              isUploadBaFirst: false
            })
          }

          var car = res.data.data.order.cars;
          if (car != null && car != "") {
            //设置默认车辆图片信息
            that.setData({
              car: car,
              isUploadBaFirst: false,
              isUploadFirst: false,
              licenseFr: car.wdxlicensePic,
              licenseBa: car.wdxlicensePicB
            })
          }

          that.setOrderData();
        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },
  setOrderData() {
    var that = this;
    //订单信息
    var order = that.data.order;
    var typeId = 0;
    var carIndex = 0;
    for (var i = 0; i, that.data.typeList.length; i++) {
      var item = that.data.typeList[i];
      if (item.typeId == order.service.stepCarServiceType.typeId) {
        typeId = i;
        break;
      }
    }

    for (var i = 0; i, that.data.carList.length; i++) {
      var item = that.data.carList[i];
      if (item.carId == order.service.stepCar.carId) {
        carIndex = i;
        break;
      }
    }

    that.setData({
      typeIndex: typeId,
      user: order.order.account,
      car: order.order.cars,
      carIndex: carIndex

    })

  },
  chooseMap: function() {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          addressPick: res.address,
          locLatitudePick: res.longitude,
          locLongitudePick: res.latitude,

        })
      },
    })
  },
  chooseMap2: function() {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          addressReturn: res.address,
          locLatitudeReturn: res.longitude,
          locLongitudeReturn: res.latitude,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var item = wx.getStorageSync("userchoose");
    console.log(item);
    if (item != null&&item!="") {
      this.setData({
        user: item
      })

      if (item.licenseA != null && item.licenseB != null) {
        //设置默认车辆图片信息
        console.log(item);
        that.setData({
          idenCardA: item.licenseA,
          idenCardB: item.licenseB,
          isUploadIDFr: true,
          isUploadIDBa: true,
          isUploadFr: true,
          isUploadBa: true,
          isUploadFirst: false,
          isUploadBaFirst: false
        })
      }
    }



    var car = wx.getStorageSync("carchoose");
    if (car != null&&car!="") {
      this.setData({
        car: car,
        isUploadIDFirst: false,
        isUploadBaIDFirst: false,
        licenseFr: car.wdxlicensePic,
        licenseBa: car.wdxlicensePicB,
      })
    }

    setTimeout(function() {
      that.getCarList();
    }, 300);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("userchoose", null);
    wx.setStorageSync("carchoose", null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  selectCustomer: function(e) {
    wx.navigateTo({
      url: '../customerList/customerList',
    })
  },
  exchangeCar: function(e) {
    var id = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '../carList/carList?userId=' + id,
    })
  },
  //行驶证
  uploadFr: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseFr: tempFilePaths,
          isUploadFr: true,
          isUploadFirst: false
        })

        console.log(that.data.licenseFr);


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.licenseFr[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadFr: true,
              })
              that.data.licenseFr = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBa: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseBa: tempFilePaths,
          isUploadBa: true,
          isUploadBaFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.licenseBa[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadBa: true,
              })
              that.data.licenseBa = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
  //驾驶证正面
  uploadFrID: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardA: tempFilePaths,
          isUploadIDFr: true,
          isUploadIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.idenCardA[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadIDFr: true,
              })
              that.data.idenCardA = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBaID: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardB: tempFilePaths,
          isUploadIDBa: true,
          isUploadBaIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: openIdUrl.uploadPic,
          filePath: that.data.idenCardB[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadIDBa: true,
              })
              that.data.idenCardB = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
})